<?php
class xoGroups
{
	public static function group($id) {
		
	}
	
	public static function redirect($url, $time=3, $msg="", $status=false ) {
		/**
		if ( isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
			$_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest" &&
			$_SERVER["REQUEST_METHOD"] === "POST") {
			$arg = array(
				'status' => $status,
				'message' => $msg,
				'time' => (int)$time,
				'url' => $url,
			);
			echo json_encode($arg);
			exit;
		}**/
		redirect_header($url,$time,$msg);
	}
	/**
	 * Check post fields
	 *
	 * @param string $classname
	 * @param array $params
	 * @param bool $token
	 * @return $msg
	 */
	public static function checkSubmitFields($classname, array $params, $token=true) {
		$classname = strtolower($classname);
		if ( empty($classname) || !file_exists(dirname(dirname(__FILE__))."/class/{$classname}.php") ) return false;
		
		if ( true === $token ) {
			if ( !$GLOBALS["xoopsSecurity"]->check() ) {
				$msg = "抱歉，页面已过期!";
				return $msg;
			}
		}
		
		$msg = array();
		
		$check_handler = xoops_getmodulehandler($classname);
		$check_obj = $check_handler->create();
		$check_vars = $check_obj->vars;
		
		foreach ( $params as $k=>$val ) {
			if ( isset($check_vars[$k]["required"]) && !trim($val) ) {
				$msg[] = "字段 {$k} 不能为空";
			}
			
			if ( isset($check_vars[$k]["required"]) && trim($val) &&
				 $check_vars[$k]["data_type"] != self::checkFieldType($val,$check_vars[$k]["data_type"]) ) {
				$msg[] = "字段 {$k} 类型错误";
			}
		}
		
		if ( empty($msg) ) {
			return true;
		}
		return implode("<br />",$msg);
	}

	/**
	 * Enter description here...
	 *  define('XOBJ_DTYPE_TXTBOX',     1);
	 *  define('XOBJ_DTYPE_TXTAREA',    2);
	 *  define('XOBJ_DTYPE_INT',        3);
	 *  define('XOBJ_DTYPE_URL',        4);
	 *  define('XOBJ_DTYPE_EMAIL',      5);
	 *  define('XOBJ_DTYPE_ARRAY',      6);
	 *  define('XOBJ_DTYPE_OTHER',      7);
	 *  define('XOBJ_DTYPE_SOURCE',     8);
	 *  define('XOBJ_DTYPE_STIME',      9);
	 *  define('XOBJ_DTYPE_MTIME',      10);
	 *  define('XOBJ_DTYPE_LTIME',      11);
	 * @param string $val
	 * @param int $type
	 * @return int $val 
	 */
	public static function checkFieldType($val, $type=1) {
		if ( empty($val) || empty($type) ) return false;
		switch ($type) {
			case XOBJ_DTYPE_TXTBOX:
				if ( true === is_string($val) ) {
					return XOBJ_DTYPE_TXTBOX;
				}
				break;
				
			case XOBJ_DTYPE_TXTAREA:
				if ( true === is_string($val) ) {
					return XOBJ_DTYPE_TXTAREA;
				}
				break;
				
			case XOBJ_DTYPE_OTHER:
				if ( true === is_string($val) ) {
					return XOBJ_DTYPE_OTHER;
				}
				break;
				
			case XOBJ_DTYPE_INT:
				if ( true === is_int((int)$val) ) {
					return XOBJ_DTYPE_INT;
				}
				break;
				
			case XOBJ_DTYPE_URL:
				if ( preg_match("/^http[s]*:\/\//i", $val) ) {
					return XOBJ_DTYPE_URL; 
				}
				break;
				
			case XOBJ_DTYPE_EMAIL:
				if ( preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i",$val) ) {
					return XOBJ_DTYPE_EMAIL; 
				}
				break;
				
			case XOBJ_DTYPE_ARRAY:
				if ( is_array($val) ) {
					return XOBJ_DTYPE_ARRAY; 
				}
				break;
				
			case XOBJ_DTYPE_STIME:
				if ( strtotime($val) ) {
					return XOBJ_DTYPE_STIME;
				}
				break;
				
			case XOBJ_DTYPE_MTIME:
				if ( strtotime($val) ) {
					return XOBJ_DTYPE_MTIME;
				}
				break;
				
			case XOBJ_DTYPE_LTIME:
				if ( strtotime($val) ) {
					return XOBJ_DTYPE_LTIME;
				}
				break;
				
			default:
				return false;
				
		}
		
	}
	
	/**
     * select xoops users table, max limit 30 
     *
     * @param array $user_ids
     * @param bool $online
     * @return array $ret array("uid","name","avatar")
     */
	public static function getUsers( array $user_ids, $online=false ) {
		$ret = array();
		$user_handler = xoops_gethandler("member");
		$criteria = new CriteriaCompo();
		$criteria->add( new Criteria("uid"," ( " . implode(",",$user_ids) . " ) ","in") );
		$users = $user_handler->getUsers($criteria);
        
        if ( !empty($online) ) {
            unset($criteria);
	    	$online_handler = xoops_gethandler("online");
			$criteria = new CriteriaCompo(new Criteria("online_uid"," ( " . implode(",",$user_ids) . " ) ","in"));
			$ol_objs = $online_handler->getAll($criteria);
			$onlines = array();
			if ( $ol_objs ) {
				foreach ( $ol_objs as $k=>$obj ) {
					$onlines[$k] = true;
				}
			}
    	}
    	
        if ( !empty($users) ) {
            foreach ( $users as $k=>$obj ) {
                $ret[$obj->uid()]["uid"] = $obj->uid();
                $ret[$obj->uid()]["name"] = $obj->name() ? $obj->name() : $obj->uname() ;
                $ret[$obj->uid()]["avatar"] = $obj->user_avatar();
                $ret[$obj->uid()]["online"] = isset($onlines[$obj->uid()]) ? $onlines[$obj->uid()] : false;
            }
        }
        return $ret;
	}
	
	public static function getCriteria ( array $params ) {
		$criteria = new CriteriaCompo();
        if ( isset($params["criteria"]) && is_array($params["criteria"]) ) {
            foreach( $params["criteria"] as $k=>$val ) {
                if ( is_array($val) ) {                    
                    $criteria->add( new Criteria($k," ( " . implode(",",$val) . " ) ","in") );
                } else {
                    $criteria->add( new Criteria($k,$val) );
                }
            }
        }
        if ( isset($params["start"]) ) {
            $criteria->setStart( intval($params["start"]) );
        }
        if ( isset($params["limit"]) ) {
            $criteria->setLimit( intval($params["limit"]) );
        }
        if ( isset($params["order"]) ) {
			$criteria->setOrder( trim($params["order"]) );
        }
        if ( isset($params["sort"]) ) {
            $sort_str = $params["sort"] ;
            if ( is_array($sort_str) ) {
                $order = isset($params["order"]) ? $params["order"] : "ASC";
                $sort = "";
                $_fields = array_keys($sort_str);
                $sort .= "{$_fields["0"]} {$sort_str[$_fields["0"]]} ";
                for ( $i=1; $i<count($_fields)-1; $i++ ) {
                    $sort .= ", {$_fields[$i]} {$sort_str[$_fields[$i]]} ";
                }
                $sort .= ", {$_fields[count($sort_str)-1]}";
                $sort_str = $sort;
            } 
            $criteria->setSort( $sort_str );
        }
        return $criteria;		
	}
}